﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;

/// <Summary> 
/// Here Information regarding a particular session is displayed
/// like how many players are in this session, which level is selected etc
/// When all players say they are Ready, the fun STARTS :)
/// </Summary> 

namespace Imagine
{
    class Lobby : GameScreen
    {
        int offset;
        int count;
       public override bool Init(object extraData)
       {
           offset = 30;
           count = 0;

           return true; 
       }
       public override bool Draw(GameTime gameTime, SpriteBatch spriteBatch)
       {
           Engine.Instance().GraphicsDevice.Clear(Color.White);
           spriteBatch.DrawString(Engine.Instance().SpriteFont, "LobbyScreen", new Vector2(200, 1), Color.Chocolate);
           spriteBatch.DrawString(Engine.Instance().SpriteFont, "Players", new Vector2(200, 200), Color.Chocolate);
           spriteBatch.DrawString(Engine.Instance().SpriteFont, "Press Space to mark Ready", new Vector2(200, 100), Color.Chocolate);
           GamerCollection<NetworkGamer> gamerCollection = NetworkManager.Instance().Session.AllGamers;
           foreach (NetworkGamer gamer in gamerCollection)
           {
               Engine.Instance().SpriteBatch.DrawString(Engine.Instance().SpriteFont, gamer.Gamertag, new Vector2(200, 230 + (offset * count)), Color.Chocolate);
               count += 1;
           }
           count = 0;

           //Display Game Info like Level etc, note we are only displaying the info here
           //If we actually join the Game only then we will copy GamePlayParams
           return true; 
       }
       public override bool Update(GameTime gameTime)
       {
           NetworkManager.Instance().Session.Update();

           //Check if everyone is ready, then start the actual game
           if (NetworkManager.Instance().Session.IsEveryoneReady && NetworkManager.Instance().Session.IsHost && NetworkManager.Instance().Session.SessionState != NetworkSessionState.Playing)
           {
               //It only changes SessionState to Playing
               //All other Local gamers can detect it and then start the game
               NetworkManager.Instance().Session.StartGame();
           }

           if (NetworkManager.Instance().Session.SessionState == NetworkSessionState.Playing)
               ScreenManager.Instance().SetScreen(ImagineScreens.eGamePlayScreen, ScreenState.Dead, null);

           if (InputManager.Instance().IsKeyDown(Keys.Space))
           {
               foreach (LocalNetworkGamer gamer in NetworkManager.Instance().Session.LocalGamers)
               {
                   if (NetworkManager.Instance().Session.SessionState != NetworkSessionState.Playing)
                   gamer.IsReady = true;
               }
           }

           return true; 
       }
       public override bool Release()
       {
           return true; 
       }
    }
}
